package com.yanqu.road.server.pb;

import com.yanqu.road.entity.config.vehicle.VehicleInfo;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.*;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.tradewar.UserTradeWarInfo;
import com.yanqu.road.logic.pb.*;
import com.yanqu.road.pb.beauty.BeautyProto;
import com.yanqu.road.pb.building.BuildingProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.vehicle.VehicleProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.BeautyMgr;
import com.yanqu.road.server.manger.config.VehicleMgr;
import com.yanqu.road.server.manger.player.TradeWarMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PlayerPb {
	public static PlayerProto.PlayerBaseTempMsg.Builder parsePlayerBaseTempMsg(UserInfo userInfo){
		PlayerProto.PlayerBaseTempMsg.Builder msg = PlayerProto.PlayerBaseTempMsg.newBuilder();
		msg.setUserAliasId(userInfo.getUserAliasId());
		String nickName = StringUtils.clearHeadAndTailEmpty(userInfo.getNickName());
		msg.setNickName(nickName);
		msg.setTitleId(userInfo.getTitleId());
		msg.setDecoration(UserMgr.getDecoration(userInfo.getUserId()));
		msg.setUnionName(UserMgr.getUnionName(userInfo.getUserId()));
		msg.setUnionUid(userInfo.getUnionUid());
		msg.setUnionAliasId(UserMgr.getUnionAliasId(userInfo.getUserId()));
		msg.setVipExp(userInfo.getVipExp());
		msg.setSex(userInfo.getSex());
		msg.setEarnSpeed(userInfo.getEarnSpeed().toString());
		msg.setRegisterTime(userInfo.getRegisterTime());
		msg.setServerId(GameServer.getInstance().getServerId());
		msg.setWorld(userInfo.getWorld());
		msg.setContinent(userInfo.getContinent());
		msg.setCountry(userInfo.getCountry());
		msg.setProvince(userInfo.getProvince());
		msg.setCity(userInfo.getCity());
		msg.setLastLoginTime(userInfo.getLastLoginTime());
		msg.setPrestige(userInfo.getPrestige().toString());
		msg.setHideVipAndAddress(userInfo.getHideVipAndAddress());
		msg.setTotalAbility(userInfo.getTotalAbility().toString());
		msg.setTotalIntimacy(userInfo.getTotalIntimacy().toString());
		msg.setBadge(userInfo.getBadge());
		return msg;
	}

	public static PlayerProto.PlayerDataTempMsg.Builder parsePlayerDataTempMsg(UserInfo userInfo, Map<ePlayerAttrType, UserAttribute> userAttrMap) {
		if(null == userInfo){
			return null;
		}
		PlayerProto.PlayerDataTempMsg.Builder msg = PlayerProto.PlayerDataTempMsg.newBuilder();
		msg.setUserId(userInfo.getUserId());
		PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerPb.parsePlayerBaseTempMsg(userInfo);
		msg.setPlayerBaseData(baseMsg);
		long ingots = 0;
		if (userAttrMap.containsKey(ePlayerAttrType.Ingots)) {
			ingots = userAttrMap.get(ePlayerAttrType.Ingots).getValue();
		}
		msg.setIngots(ingots);
		msg.setSilver(userInfo.getSilver().toString());
		msg.setPatronsExp(userInfo.getPatronsExp().toString());
		ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
		if (serverInfo != null) {
			if (eChannelType.isMainLandChannel(serverInfo.getChannelId())) {
				String location = userInfo.getIpLocation();
				if (location != null) {
					msg.setIpAddress(location);
				}
			}
		}
		if (userAttrMap.containsKey(ePlayerAttrType.DefaultSkin)) {
			msg.setDefaultSkin(Math.toIntExact(userAttrMap.get(ePlayerAttrType.DefaultSkin).getValue()));
		}
		return msg;
	}

	public static PlayerProto.PlayerSimpleTempMsg.Builder parsePlayerSimpleTempMsg(UserInfo userInfo) {
		if(null == userInfo){
			return null;
		}
		PlayerProto.PlayerSimpleTempMsg.Builder msg = PlayerProto.PlayerSimpleTempMsg.newBuilder();
		msg.setUserId(userInfo.getUserId());
		PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerPb.parsePlayerBaseTempMsg(userInfo);
		msg.setPlayerBaseData(baseMsg);
		GamePlayer player = GamePlayerMgr.getOnlinePlayer(userInfo.getUserId());
		if(null != player && player.getState() == PlayerState.ONLINE) {
			msg.setIsOnline(true);
		}else {
			msg.setIsOnline(false);
		}
		UserTradeWarInfo userTradeWarInfo = TradeWarMgr.getUserTradeWarInfo(userInfo.getUserId());
		long score = 0;
		if(null != userTradeWarInfo){
			score = userTradeWarInfo.getScore();
		}
		msg.setTradeWarScore(score);
		msg.setPrestige(userInfo.getPrestige().toString());
		return msg;
	}

	//跨服盐场的查询，因为用户不是本服的所以没有userInfo,在线等字段信息也没有，兼容之前的本服盐场这几个参数瞎鸡巴传就可以了，客户端也没有用到
	public static PlayerProto.PlayerSimpleTempMsg.Builder parsePlayerSimpleTempMsg(long userId,UserBaseInfo userBaseInfo) {
		if(userId == 0 || null == userBaseInfo){
			return null;
		}
		PlayerProto.PlayerSimpleTempMsg.Builder msg = PlayerProto.PlayerSimpleTempMsg.newBuilder();
		msg.setUserId(userId);
		PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo);
		msg.setPlayerBaseData(baseMsg);
		msg.setIsOnline(true);
		long score = 0;
		msg.setTradeWarScore(score);
		msg.setPrestige("0");
		return msg;
	}

	public static PlayerProto.WatchPlayerSimpleRespMsg.Builder parseWatchPlayerSimpleRespMsg(WatchPlayerResult watchPlayerResult){
		PlayerProto.WatchPlayerSimpleRespMsg.Builder msg = PlayerProto.WatchPlayerSimpleRespMsg.newBuilder();
		msg.setUserData(PlayerPb.parsePlayerSimpleTempMsg(watchPlayerResult.getWatchUserInfo()));
		msg.setEstateType(watchPlayerResult.getEstateType());
		msg.setEstatePos(watchPlayerResult.getEstatePos());
		int heartCount = watchPlayerResult.getWatchUserInfo().getHeartCount();
		BigInteger ability = BigInteger.ZERO;
		if (null != watchPlayerResult.getBuildingMap()) {
			for (UserBuilding userBuilding : watchPlayerResult.getBuildingMap().values()) {
				BuildingProto.BuildingTempMsg.Builder buildingMsg = BuildingPb.parseBuildingTempMsg(userBuilding);
				if (null != buildingMsg) {
					msg.addBuildingList(buildingMsg);
				}
			}
		}
		if (null != watchPlayerResult.getPatronsMap()) {
			for (UserPatrons userPatrons : watchPlayerResult.getPatronsMap().values()) {
				PatronsProto.PatronsSimpleTempMsg.Builder patronsMsg = PatronsPb.parsePatronsSimpleTempMsg(userPatrons);
				if (null != patronsMsg) {
					msg.addPatronsList(patronsMsg);
				}
				ability = ability.add(userPatrons.getAbility());
			}
		}
		if (null != watchPlayerResult.getVehicleMap()) {
			List<UserVehicle> vehicleList = new ArrayList<>();
			for (UserVehicle userVehicle : watchPlayerResult.getVehicleMap().values()) {
				if (VehicleMgr.isUserVehicleExist(userVehicle)) {
					vehicleList.add(userVehicle);
				}
			}
			if (vehicleList.size() > 100) {//超过100只珍兽，只发品质靠前的100只
				vehicleList.sort((o1, o2) -> {
					VehicleInfo vehicleInfo1 = VehicleMgr.getVehicleInfo(o1.getTemplateId());
					VehicleInfo vehicleInfo2 = VehicleMgr.getVehicleInfo(o2.getTemplateId());
					return vehicleInfo2.getQuality() - vehicleInfo1.getQuality();
				});
				vehicleList = vehicleList.subList(0, 100);
			}
			for (UserVehicle userVehicle : vehicleList) {
				VehicleProto.VehicleSimpleTempMsg.Builder vehicleMsg = VehiclePb.parseVehicleSimpleTempMsg(userVehicle);
				msg.addVehicleList(vehicleMsg);
			}
		}
		if (null != watchPlayerResult.getBeautyMap()) {
			for (UserBeauty userBeauty : watchPlayerResult.getBeautyMap().values()) {
				if (BeautyMgr.isUserBeautyExist(userBeauty)) {
					BeautyProto.BeautySimpleTempMsg.Builder beautyMsg = BeautyPb.parseBeautySimpleTempMsg(userBeauty);
					if (null != beautyMsg) {
						msg.addBeautyList(beautyMsg);
					}
				}
			}
		}
		msg.setBankLv(watchPlayerResult.getWatchUserInfo().getHouseLv());
		msg.setHeartCount(heartCount);
		msg.setPatronsAbility(ability.toString());
		msg.setUnlockAssistant(watchPlayerResult.getAssistantUnlock());
		msg.setEstateSkinId(watchPlayerResult.getEstateSkinId());
		msg.setBankSkinId(watchPlayerResult.getWatchUserInfo().getBankSkinId());
		return msg;
	}

	public static PlayerProto.SystemOpenPreviewTempMsg.Builder parseSystemOpenPreviewTempMsg(UserSystemInfo data){
		if(null == data){
			return null;
		}
		PlayerProto.SystemOpenPreviewTempMsg.Builder msg = PlayerProto.SystemOpenPreviewTempMsg.newBuilder();
		msg.setSystemId(data.getSystemId());
		msg.setIsReward(data.isGetPreviewReward());
		return msg;
	}

	public static PlayerProto.SystemOpenPreviewListMsg.Builder parseSystemOpenPreviewListMsg(List<UserSystemInfo> dataList){
		PlayerProto.SystemOpenPreviewListMsg.Builder listMsg = PlayerProto.SystemOpenPreviewListMsg.newBuilder();
		if(null != dataList){
			for(UserSystemInfo data : dataList){
				PlayerProto.SystemOpenPreviewTempMsg.Builder msg = parseSystemOpenPreviewTempMsg(data);
				if(null != msg){
					listMsg.addDataList(msg);
				}
			}
		}
		return listMsg;
	}

	/**
	 * build玩家基础信息
	 * @param userInfo
	 * @return
	 */
	public static PlayerProto.PlayerBaseInfoTemp.Builder parsePlayerBaseInfoTemp(UserInfo userInfo) {
		PlayerProto.PlayerBaseInfoTemp.Builder builder = PlayerProto.PlayerBaseInfoTemp.newBuilder();
		PlayerProto.PlayerBaseTempMsg.Builder playerBaseTempMsg = parsePlayerBaseTempMsg(userInfo);
		builder.setUserId(userInfo.getUserId());
		builder.setPlayerBaseData(playerBaseTempMsg);
		return builder;
	}
}
